<template>
  <div class="header" :class="className">
    <div class="title">
      <div v-if="showIcon" class="header-title-icon">
        <el-tooltip content="点击上传" placement="top">
          <i class="el-icon-discover" />
        </el-tooltip>
      </div>

      <!-- 标题 -->
      <div v-show="!editTitle" class="header-title-text" :style="{ fontSize: bigFontSize ? '20px' : '18px' }">
        {{ title2 }}
      </div>

      <!-- 编辑标题 input -->
      <div v-show="editTitle">
        <el-input ref="input" v-model="title2" class="edit-title-input" style="width: 100px" @blur="handleInputBlur" />
      </div>

      <!-- 编辑标题 icon -->
      <div v-if="showEdit" class="header-title-edit" @click="handleEditTitle">
        <el-tooltip content="编辑标题" placement="top">
          <i class="el-icon-edit-outline" />
        </el-tooltip>
      </div>
    </div>

    <!-- 右侧添加 -->
    <slot />
  </div>
</template>

<script>
  export default {
    name: 'Header',
    props: {
      className: {
        type: String,
        default: '',
      },
      showEdit: {
        type: Boolean,
        default: true,
      },
      editTitle: {
        type: Boolean,
        default: false,
      },
      title: {
        type: String,
        default: '',
      },
      showIcon: {
        type: Boolean,
        default: false,
      },
      bigFontSize: {
        type: Boolean,
        default: false,
      },
    },
    data() {
      return {
        title2: '',
      }
    },
    created() {
      this.title2 = this.title
    },
    mounted() {
      this.$nextTick(() => {
        this.$refs.input.focus()
      })
    },
    methods: {
      handleEditTitle() {
        this.$emit('handleEditTitle')
        this.$nextTick(() => {
          this.$refs.input.focus()
        })
      },
      handleInputBlur() {
        this.$emit('handleInputBlur', this.title2)
      },
    },
  }
</script>

<style scoped lang="scss">
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 0 0 10px;

    .title {
      display: flex;
      align-items: center;

      .header-title-icon {
        width: 25px;
        height: 25px;
        text-align: center;
        line-height: 25px;
        background: #2f74ff;
        border-radius: 35%;
        margin-right: 10px;
        cursor: pointer;

        i {
          color: #ffffff;
        }
      }

      .header-title-edit {
        //width: 14px;
        //height: 14px;
        cursor: pointer;
        //margin-right: 5px;
        align-self: flex-end;
      }

      .header-title-text {
        font-weight: bold;
        margin: 0 5px 0 0;
      }

      .edit-title-input {
        ::v-deep .el-input__inner {
          height: 23px;
          line-height: 23px;
        }
      }
    }

    .right {
      display: flex;
      align-items: center;
    }
  }
</style>
